﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;
using WXYW.Model;

namespace WXYW.BLL
{
    public class GatheringService : RepositoryBase
    {
        public IQueryable<Gathering> GetGatheringsByYearMonthService(int year, int month, int servicestation)
        {
            return GetEntities<Gathering>(g => g.PayDateTime.Value.Year == year && g.PayDateTime.Value.Month == month
                && g.ApplicationForm.ServiceStation.ID == servicestation);
        }

        public IQueryable<Gathering> GetGatheringsByYearMonthService(int year, int month)
        {
            return GetEntities<Gathering>(g => g.PayDateTime.Value.Year == year && g.PayDateTime.Value.Month == month);
        }

        public IQueryable<Gathering> GetGatheringsByYearMonthService(int year, int month, int tyear, int tmonth, int servicestation)
        {
            if (servicestation > 0)
            {
                return GetEntities<Gathering>(g => g.PayDateTime.Value.Year >= year && g.PayDateTime.Value.Month >= month && g.PayDateTime.Value.Year <= tyear && g.PayDateTime.Value.Month <= tmonth
                    && g.ApplicationForm.ServiceStation.ID == servicestation);
            }
            else
            {
                return GetEntities<Gathering>(g => g.PayDateTime.Value.Year >= year && g.PayDateTime.Value.Month >= month && g.PayDateTime.Value.Year <= tyear && g.PayDateTime.Value.Month <= tmonth);
            }
        }

        public IQueryable<Gathering> GetGatheringsByYear(int year)
        {
            return GetEntities<Gathering>(g => g.PayDateTime.Value.Year == year);
        }

        public List<SFTJModel> GetSFTJModelBy(int year, int month, int tyear, int tmonth, int servicesataion)
        {
            List<SFTJModel> sftjModels = new List<SFTJModel>();
            ServiceStationService ssService = new ServiceStationService();
            DateTime startDate = new DateTime(year, month, 1);
            if (tmonth == 12)
            {
                tyear += 1;
                tmonth = 1;
            }
            else
            {
                tmonth += 1;
            }
            DateTime endDate = new DateTime(tyear, tmonth, 1);
            IQueryable<Gathering> gatherings = GetEntities<Gathering>(g => g.PayDateTime.Value >= startDate && g.PayDateTime.Value < endDate);

            if (servicesataion != 0)
            {
                gatherings = gatherings.Where(g => g.ApplicationForm.ServiceStation.ID == servicesataion);
                SFTJModel model = new SFTJModel();
                double? bwba = gatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("保外备案")).Count();
                double? bxpx = gatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("保修凭证")).Count();
                double? tf = gatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("特服")).Count();
                double? zcwx = gatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("正常收费")).Sum(g => g.RealPay);
                model.ServiceStation = ssService.GetServiceStation(servicesataion).Name;
                model.BWBA = bwba.HasValue ? bwba.Value : 0;
                model.BXPX = bxpx.HasValue ? bxpx.Value : 0;
                model.TF = tf.HasValue ? tf.Value : 0;
                model.ZCWX = zcwx.HasValue ? zcwx.Value : 0;
                sftjModels.Add(model);
            }
            else
            {
                List<ServiceStation> serviceStations = ssService.GetALLServiceStations().ToList();
                foreach (ServiceStation station in serviceStations)
                {
                    IQueryable<Gathering> sGatherings = gatherings.Where(g => g.ApplicationForm.ServiceStation.ID == station.ID);
                    double? bwba = sGatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("保外备案")).Count();
                    double? bxpx = sGatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("保修凭证")).Count();
                    double? tf = sGatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("特服")).Count();
                    double? zcwx = sGatherings.Where(g => g.ReasonOfNotReceiveMoney.Equals("正常收费")).Sum(g => g.RealPay);
                    SFTJModel model = new SFTJModel();
                    model.ServiceStation = station.Name;
                    model.BWBA = bwba.HasValue ? bwba.Value : 0;
                    model.BXPX = bxpx.HasValue ? bxpx.Value : 0;
                    model.TF = tf.HasValue ? tf.Value : 0;
                    model.ZCWX = zcwx.HasValue ? zcwx.Value : 0;
                    sftjModels.Add(model);
                }
            }
            return sftjModels;
        }
    }
}
